﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XUCore.Extensions;
using XUCore.WeChat.Enterprise.Apis.Ticket;

namespace XUCore.WeChat.Enterprise
{
    /// <summary>
    /// 企业微信js ticket管理器
    /// </summary>
    public class EnterpriseWeChatTicketManager : IEnterpriseWeChatTicketManager
    {
        private readonly EnterpriseWeChatFuncs weChatFuncs;
        private readonly IServiceProvider serviceProvider;
        private readonly IEnterpriseWeChatTokenManager tokenManager;
        /// <summary>
        /// 企业微信js ticket管理器
        /// </summary>
        /// <param name="weChatFuncs"></param>
        /// <param name="tokenManager"></param>
        /// <param name="serviceProvider"></param>
        public EnterpriseWeChatTicketManager(EnterpriseWeChatFuncs weChatFuncs, IEnterpriseWeChatTokenManager tokenManager, IServiceProvider serviceProvider)
        {
            this.weChatFuncs = weChatFuncs;
            this.serviceProvider = serviceProvider;
            this.tokenManager = tokenManager;
        }

        /// <summary>
        /// 获取Agent ticket
        /// </summary>
        /// <param name="suiteId"></param>
        /// <param name="corpId"></param>
        /// <param name="agentId"></param>
        /// <returns></returns>
        public virtual async Task<string> GetAgentTicketAsync(string suiteId, string corpId, int agentId)
        {
            string ticket = weChatFuncs?.GetEnterpriseWeChatTicketByAppId($"Agent-{suiteId}{corpId}{agentId}");

            if (string.IsNullOrEmpty(ticket))
            {
                var accessToken = await tokenManager.GetAccessTokenAsync(suiteId, corpId, agentId);

                var ticketApi = serviceProvider.GetService<IEnterpriseTicketApi>();
                var result = await ticketApi.GetAgentAsync(accessToken);
                if (!result.IsSuccess())
                {
                    throw new WeChatSdkException($"suiteId：{suiteId} corpId：{corpId} agentId：{agentId} {result.Message}");
                }
                weChatFuncs?.CacheEnterpriseWeChatTicket($"Agent-{suiteId}{corpId}{agentId}", result.ticket);
                return result.ticket;
            }

            return ticket;
        }

        /// <summary>
        /// 获取Enterprise ticket
        /// </summary>
        /// <param name="suiteId"></param>
        /// <param name="corpId"></param>
        /// <param name="agentId"></param>
        /// <returns></returns>
        public virtual async Task<string> GetEnterpriseTicketAsync(string suiteId, string corpId, int agentId)
        {
            string ticket = weChatFuncs?.GetEnterpriseWeChatTicketByAppId($"Enterprise-{suiteId}{corpId}");

            if (string.IsNullOrEmpty(ticket))
            {
                var accessToken = await tokenManager.GetAccessTokenAsync(suiteId, corpId, agentId);

                var ticketApi = serviceProvider.GetService<IEnterpriseTicketApi>();
                var result = await ticketApi.GetEnterpriseAsync(accessToken);
                if (!result.IsSuccess())
                {
                    throw new WeChatSdkException($"suiteId：{suiteId} corpId：{corpId} agentId：{agentId} {result.Message}");
                }
                weChatFuncs?.CacheEnterpriseWeChatTicket($"Enterprise-{suiteId}{corpId}", result.ticket);
                return result.ticket;
            }

            return ticket;
        }
    }

}
